package com.digitalchina.lxnsy.api;

import android.app.Activity;
import android.os.Handler;

import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.toolkit.OkHttpUtil;
import com.digitalchina.lxnsy.xml.RongCloudXML;
import com.digitalchina.lxnsy.xml.UserXML;
import com.lzy.okhttputils.model.HttpParams;
import com.ta.utdid2.android.utils.StringUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.api
 * 创建人：白彦东
 * 创建时间：2017年1月6日 15:45:26
 * 修改备注：
 * ================================================
 */

public class BusinessCollect {

    /**
     * 收藏
     *
     * @param activity
     * @param title     标题
     * @param picture   数据图片
     * @param type      收藏类型 商品-6 文章-7 视频-8 商户-9
     * @param contentId 收藏ID
     * @param price     收藏价格
     * @param logo      商户图片
     * @param intro     简介
     * @param url       地址
     * @param handler
     */
    public static void store(Activity activity, String title, String picture, String type,
                             String contentId, String price, String logo, String intro,
                             String url, Handler handler) {

        HttpParams params = new HttpParams();
        params.put("title", title);
        params.put("picture", picture);
        params.put("type", type);
        params.put("contentId", contentId);
        params.put("price", price);
        params.put("logo", logo);
        params.put("intro", intro);
        params.put("url", url);
        params.put("user", UserXML.getUserId(activity));

        OkHttpUtil.post(activity, URL.URL_STORE, "", params, handler, MSG.MSG_STORE_SUCCESS, MSG.MSG_STORE_FIELD);

    }

    /**
     * 取消收藏
     *
     * @param activity
     * @param contentId 收藏内容的id 必填
     * @param handler
     */
    public static void cancelStore(Activity activity, String contentId, Handler handler) {
        HttpParams params = new HttpParams();
        params.put("contentId", contentId);
        params.put("user", UserXML.getUserId(activity));

        OkHttpUtil.post(activity, URL.URL_CANCEL_STORE, "", params, handler,
                MSG.MSG_CANCEL_STORE_SUCCESS, MSG.MSG_CANCEL_STORE_FIELD);
    }

    /**
     * 是否已收藏
     *
     * @param activity
     * @param contentId 收藏内容的id 必填
     * @param handler
     */
    public static void isCollectApi(Activity activity, String contentId, Handler handler) {
        HttpParams params = new HttpParams();
        params.put("contentId", contentId);
        params.put("user", UserXML.getUserId(activity));

        OkHttpUtil.post(activity, URL.URL_IS_STORE, "", params, handler,
                MSG.MSG_IS_STORE_SUCCESS, MSG.MSG_IS_STORE_FIELD);
    }


    /**
     * 我的收藏列表
     *
     * @param activity
     * @param type     收藏类型 15-资讯 16-活动 17-共享 18-商户
     * @param page
     * @param limit
     * @param handler
     */
    public static void getUserStores(Activity activity, String type, int page, int limit, Handler handler) {
        HttpParams params = new HttpParams();
        params.put("createdBy", UserXML.getUserId(activity));
        params.put("type", type);
        params.put("page", String.valueOf(page));
        params.put("perPage", String.valueOf(limit));


        OkHttpUtil.get(activity, URL.URL_GET_USER_STORES, "", params, handler,
                MSG.MSG_GET_USER_STORES_SUCCESS, MSG.MSG_GET_USER_STORES_FIELD);
    }


    /**
     * 添加收藏
     *
     * @param activity
     * @param contentId  收藏内容的id
     * @param type       收藏类型 0,共享 1，视频 2，活动 3，资讯
     * @param handler
     */
    public static void addStore(Activity activity, String contentId, String type, Handler handler) {
        try {
            final JSONObject params = new JSONObject();
            params.put("recordId", contentId);
            params.put("type", type);
            params.put("tenantCode", StringUtils.isEmpty(RongCloudXML.getTenantCode(activity))?"T000010":RongCloudXML.getTenantCode(activity));
            params.put("subjectInfoId", RongCloudXML.getSubjectInfoId(activity));

            OkHttpUtil.post(activity, URL.URL_ADD_STORE, "", params, handler,
                    MSG.MSG_ADD_STORE_SUCCESS, MSG.MSG_ADD_STORE_FIELD);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    /**
     * 取消收藏
     *
     * @param activity
     * @param contentId 收藏内容的id
     * @param type      收藏类型 15-资讯 16-活动 17-共享 18-商户
     * @param handler
     */
    public static void callOffStore(Activity activity, String contentId, String type, Handler handler) {
        final JSONObject params = new JSONObject();
        OkHttpUtil.postDelete(activity, URL.URL_CALL_OFF_STORE+contentId+"/"+UserXML.getUserId(activity), "", params, handler,
                MSG.MSG_CALL_OFF_STORE_SUCCESS, MSG.MSG_CALL_OFF_STORE_FIELD);
    }


}
